package com.example.demo1.Controller;

import com.example.demo1.Bean.UserBean;
import com.example.demo1.Bean.UserStates;
import com.example.demo1.Service.UserService;
import com.example.demo1.Service.UserstatusService;
import com.example.demo1.utils.ExcelUntil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/admin/exc")
public class ExcelContro {
    @Resource
    UserService userservice;
    @Resource
    UserstatusService userstatusService;

    List<UserStates> userstatusList = new ArrayList<>();
    List<UserStates> userstatusList1 = new ArrayList<>();
    List<UserBean> userList = new ArrayList<>();
    String userdate = null;
    String userid = null;
    String userdate1 = null;


    @RequestMapping("/userExcel")
    void goodExcel(Model model, HttpServletResponse response) throws IOException {

        List<UserBean> goodList = userservice.getAllUser();
        System.out.println(goodList);
        ExcelUntil.writeExcel(response,goodList);
    }

    @RequestMapping("/userstaExcel")
    void usersta(Model model, HttpServletResponse response) throws IOException {

        userList = userservice.getAllUser();
        for(int i = 0;i<userList.size();i++){
            userdate = String.valueOf(userList.get(i).getLastdate());
            userid = String.valueOf(userList.get(i).getUserId());
            userstatusList1 = userstatusService.SearchUserStatusByid(userid);

            System.out.println(userstatusList1);


            for(int x = 0;x<userstatusList1.size();x++){
                userdate1 = String.valueOf(userstatusList1.get(x).getDate());
                if(!userdate.equals(userdate1)){
                    userstatusList.add(userstatusList1.get(x));
                    userstatusList.get(userstatusList.size()-1).setState("否");
                    System.out.println(userstatusList);
                }
            }
        }
        ExcelUntil.writeExcel(response,userstatusList);
    }
}
